package co.fitstart.entity.courseware.fitnessgroup;

import java.util.Date;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import com.alibaba.fastjson.annotation.JSONField;
import com.demo2do.core.format.PersistentFormat;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.course.Course;

/**
 * 
 * @author mohanlan
 *
 */
@Entity
@Table(name="fitness_group")
@PersistentFormat
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class FitnessGroup {
    
    @Id
    @GeneratedValue
    private Long id;
    
    /** 分组名称 */
    private String name;
    
    /** 分组简介 */
    @JSONField(serialize = false)
    private String summary;
    
    /** 分组类型 */
    @Enumerated(EnumType.ORDINAL)
    private FitnessGroupType type;
    
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @PersistentFormat
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Course course;
    
    /** 分组周数(课件分组) */
    private int weeks;
    
    /** 分组课时(线下) */
    private Integer hours;
    
    private Date createTime;
    
    private Date updateTime;
    
    @Column(name = "is_disabled")
    @JSONField(serialize = false)
    private boolean disabled;
    
    /**
     * The default constructor
     */
    public FitnessGroup() {
    }
    
    /**
     * 
     * @return
     */
    public FitnessGroup initilization() {
        if(!this.type.isSpecialty()) {
            if(this.course.isOnline()) {
                this.weeks = 6;
                this.hours = 1;
            } else if (this.course.isYear()) {
                this.weeks = 6;
                this.hours = 12;
            } else {
                this.weeks = this.course.getWeeks();
                this.hours = this.course.getHours();
            }
        } else {
            this.weeks = 1;
            this.hours = 1;
        }
        
        this.createTime = new Date();
        this.updateTime = new Date();
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    public FitnessGroup(FitnessGroup fitnessGroup, String name) {
        this.name = name;
        this.summary = fitnessGroup.getSummary();
        this.type = fitnessGroup.getType();
        this.course = fitnessGroup.getCourse();
        this.weeks = fitnessGroup.getWeeks();
        this.hours = fitnessGroup.getHours();
        this.createTime = new Date();
        this.updateTime = new Date();
        this.disabled = false;
    }
    
    /**
     * 
     * @return
     */
    public FitnessGroup edit() {
        this.updateTime = new Date();
        return this;
    }
    
    /**
     * 
     * @return
     */
    public FitnessGroup disable() {
        this.disabled = true;
        return this;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getSummary() {
        return summary;
    }

    public void setSummary(String summary) {
        this.summary = summary;
    }

    public FitnessGroupType getType() {
        return type;
    }

    public void setType(FitnessGroupType type) {
        this.type = type;
    }

    public Course getCourse() {
        return course;
    }

    public void setCourse(Course course) {
        this.course = course;
    }

    public int getWeeks() {
        return weeks;
    }

    public void setWeeks(int weeks) {
        this.weeks = weeks;
    }

    public Integer getHours() {
        return hours;
    }

    public void setHours(Integer hours) {
        this.hours = hours;
    }

    public Date getCreateTime() {
        return createTime;
    }

    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    public Date getUpdateTime() {
        return updateTime;
    }

    public void setUpdateTime(Date updateTime) {
        this.updateTime = updateTime;
    }

    public boolean isDisabled() {
        return disabled;
    }

    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
